package com.study.cloudAlibaba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.cloudAlibaba.dao.StorageServiceDao;
import com.study.cloudAlibaba.domain.Storage;
import com.study.cloudAlibaba.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author lzc
 * @date 2022/7/29 16:42
 */
@Service
public class StorageServiceImpl implements StorageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class);

    @Resource
    private StorageServiceDao storageServiceDao;

    @Override
    public void decrease(Long productId, Integer count) {
        LOGGER.info("------->storage-service中扣减库存开始");
        //模拟超时异常
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LambdaQueryWrapper<Storage> wrapper = new LambdaQueryWrapper<Storage>().eq(Storage::getProductId, productId);
        Storage storage = storageServiceDao.selectOne(wrapper);
        storage.setUsed(storage.getUsed()+count);
        storage.setResidue(storage.getResidue()-count);
        storageServiceDao.updateById(storage);
        LOGGER.info("------->storage-service中扣减库存结束");

    }
}
